package org.jeecg.modules.fk.service;

import com.alibaba.fastjson.JSON;
import org.jeecg.modules.fk.mapper.auto.model.RiskResult;
import org.jeecg.modules.fk.vo.RiskResultVO;
import org.jeecg.modules.fk.vo.indicator.IndicatorTemplateVO;
import org.jeecg.modules.fk.constant.ZbTypeEnum;
import org.jeecg.modules.fk.repository.FkDaoRepository;
import org.jeecg.modules.fk.utils.VelocityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 风险结果 相关
 */
@Slf4j
@Service
public class RiskResultService {

    @Autowired
    private FkDaoRepository daoRepository;

    @Autowired
    private RiskIndicatorService riskIndicatorService;

    private static final HashMap<String, String> RISK_INDICATOR_FORMULA_MAPPING = new HashMap<>();

    static {
        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB01-001", "流动资产-流动负债");
        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB01-002", "流动资产÷流动负债");
        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB01-003", "速动资产÷流动负债");
        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB01-004", "经营活动现金流量净额÷流动负债");
        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB01-005", "（总负债÷总资产）×100%");
        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB01-006", "[非流动负债÷（非流动负债+股东权益）]×100%");
        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB01-007", "总负债÷股东权益");
        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB01-008", "总资产÷股东权益");
        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB01-009", "（净利润+利息费用+所得税费用）÷利息费用");
        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB01-010", "经营活动现金流量净额÷利息费用");
        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB01-011", "经营活动现金流量净额÷负债总额");

        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB02-001", "应收账款周转次数 = 营业收入÷应收账款" + System.lineSeparator() +
                "应收账款周转天数 = 365÷（营业收入÷应收账款）" + System.lineSeparator() +
                "应收账款与收入比 = 应收账款÷营业收入");

        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB02-002", "存货周转次数 = 营业收入÷存货" + System.lineSeparator() +
                "存货周转天数 = 365÷（营业收入÷存货）" + System.lineSeparator() +
                "存货与收入比 = 存货÷营业收入");

        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB02-003", "流动资产周转次数 = 营业收入÷流动资产" + System.lineSeparator() +
                "流动资产周转天数 = 365÷（营业收入÷流动资产）" + System.lineSeparator() +
                "流动资产与收入比 = 流动资产÷营业收入");

        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB02-004", "非流动资产周转次数 = 营业收入÷非流动资产" + System.lineSeparator() +
                "非流动资产周转天数 = 365÷（营业收入÷非流动资产）" + System.lineSeparator() +
                "非流动资产与收入比 = 非流动资产÷营业收");

        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB02-005", "总资产周转次数 = 营业收入÷总资产" + System.lineSeparator() +
                "总资产周转天数 = 365÷（营业收入÷总资产）" + System.lineSeparator() +
                "总资产与收入比 = 总资产÷营业收入");

        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB03-001", "净利润÷营业收入×100%");
        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB03-002", "净利润÷总资产×100%");
        RISK_INDICATOR_FORMULA_MAPPING.put("CWBB03-003", "净利润÷股东权益×100%");
    }

    /**
     * 获取风险结果数据
     *
     * @param nsrsbh 纳税人识别号
     * @param skssqq 税款所属期起
     * @param skssqz 税款所属期止
     */
    public Map<String, RiskResultVO> getriskResultMap(ZbTypeEnum zbType, String nsrsbh, String skssqq, String skssqz) {
        //获取模板内容
        Map<String, IndicatorTemplateVO> templateMap = this.riskIndicatorService.getTemplateStrMap(zbType);

        List<RiskResult> riskResultList = this.daoRepository.getRiskResultExtMapper().getRiskResultList(zbType.toString(), nsrsbh, skssqq, skssqz);
        Map<String, RiskResultVO> riskResultMap = riskResultList.stream()
                .map(e -> {
                    RiskResultVO riskResultVO = new RiskResultVO();
                    BeanUtils.copyProperties(e, riskResultVO);

                    // TODO：Add log
//                    log.info("RiskResultService.getriskResultMap, templateMap:{}", templateMap);

                    //模板
                    String templateStr = templateMap.get(e.getYwmsbh()).getYwmsMb();
                    // 应对建议使用risk_indicator_template表的ydjy
                    riskResultVO.setYdjy(templateMap.get(e.getYwmsbh()).getYdjy());
                    if (StringUtils.isBlank(templateStr)) {
                        return riskResultVO;
                    }
                    String zbJsonStr = e.getZbJson();
                    Map<String, Object> zbJsonMap = StringUtils.isBlank(zbJsonStr) ? new HashMap<>(1) : JSON.parseObject(zbJsonStr, Map.class);

                    //解析数据
                    String zbStr = VelocityUtil.renderFromStr(templateStr, zbJsonMap);

                    riskResultVO.setZbResult(zbStr);

                    String zbbh = e.getZbbh();
                    if (false == ObjectUtils.isEmpty(zbbh)) {
                        String formula = RISK_INDICATOR_FORMULA_MAPPING.get(zbbh);
                        riskResultVO.setFormula(formula);
                    }

                    return riskResultVO;
                })
                .collect(Collectors.toMap(RiskResultVO::getZbbh, e -> e));

        return riskResultMap;
    }

    /**
     * 获取风险结果数据
     *
     * @param nsrsbh 纳税人识别号
     * @param skssqq 税款所属期起
     * @param skssqz 税款所属期止
     */
    public Map<String, RiskResultVO> getriskResultMap_bak(ZbTypeEnum zbType, String nsrsbh, String skssqq, String skssqz) {
        //获取模板内容
        Map<String, IndicatorTemplateVO> templateMap = this.riskIndicatorService.getTemplateStrMap(zbType);

        List<RiskResult> riskResultList = this.daoRepository.getRiskResultExtMapper().getRiskResultList(zbType.toString(), nsrsbh, skssqq, skssqz);
        riskResultList.stream()
                .map(e -> {
                    RiskResultVO riskResultVO = new RiskResultVO();
                    BeanUtils.copyProperties(e, riskResultVO);

                    //模板
                    String templateStr = templateMap.get(e.getYwmsbh()).getYwmsMb();
                    // 应对建议使用risk_indicator_template表的ydjy
                    riskResultVO.setYdjy(templateMap.get(e.getYwmsbh()).getYdjy());
                    if (StringUtils.isBlank(templateStr)) {
                        return riskResultVO;
                    }
                    String zbJsonStr = e.getZbJson();
                    Map<String, Object> zbJsonMap = StringUtils.isBlank(zbJsonStr) ? new HashMap<>(1) : JSON.parseObject(zbJsonStr, Map.class);

                    //解析数据
                    String zbStr = VelocityUtil.renderFromStr(templateStr, zbJsonMap);

                    riskResultVO.setZbResult(zbStr);
                    return riskResultVO;
                })
                .collect(Collectors.toMap(RiskResultVO::getZbbh, e -> e));

        Map<String, RiskResultVO> riskResultMap = riskResultList.stream()
                .map(e -> {
                    RiskResultVO riskResultVO = new RiskResultVO();
                    BeanUtils.copyProperties(e, riskResultVO);

                    //模板
                    String templateStr = templateMap.get(e.getYwmsbh()).getYwmsMb();
                    // 应对建议使用risk_indicator_template表的ydjy
                    riskResultVO.setYdjy(templateMap.get(e.getYwmsbh()).getYdjy());
                    if (StringUtils.isBlank(templateStr)) {
                        return riskResultVO;
                    }
                    String zbJsonStr = e.getZbJson();
                    Map<String, Object> zbJsonMap = StringUtils.isBlank(zbJsonStr) ? new HashMap<>(1) : JSON.parseObject(zbJsonStr, Map.class);

                    //解析数据
                    String zbStr = VelocityUtil.renderFromStr(templateStr, zbJsonMap);

                    riskResultVO.setZbResult(zbStr);
                    return riskResultVO;
                })
                .collect(Collectors.toMap(RiskResultVO::getZbbh, e -> e));

        return riskResultMap;
    }

}
